package com.xxl.controller;

import com.xxl.dao.Data;
import com.xxl.pojo.Employ;
import com.xxl.util.FiledConstant;
import com.xxl.util.PageUtil;
import com.xxl.util.UrlConstant;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.util.StringUtils;

/**
 * 员工信息类
 *
 * @author xxl
 * @date 2023/3/8
 */
@Controller
@RequestMapping("employ")
public class EmployController {
    private Data data;

    @Resource
    public void setData(Data data) {
        this.data = data;
    }

    @GetMapping("/list")
    public String getAllEmploys(Model model, String up, String next) {
        System.out.println("up=" + up + ",next=" + next);
        if (StringUtils.isEmpty(up) && StringUtils.isEmpty(next)) {
            if (PageUtil.page != 1) {
                PageUtil.page = 1;
            }
        }
        if (!StringUtils.isEmpty(up)) {
            //上一页
            PageUtil.up();
        }
        if (!StringUtils.isEmpty(next)) {
            //下一页
            PageUtil.next();
        }
        model.addAttribute(FiledConstant.EMPLOY_LIST_INFO, data.getAllEmploys());
        //当前页
        model.addAttribute(FiledConstant.CURRENT_PAGE, PageUtil.page);
        //总页数
        model.addAttribute(FiledConstant.TOTAL_PAGE, PageUtil.pageNumber);
        //其实没必要给跳转中心
        return "forward:" + UrlConstant.JUMP_EMPLOY_PAGE;
    }
    /**
     * 提交增加公用一个方法
     * @author xxl
     * @param
     * @return
     */
    @PostMapping("add")
    public String addEmploy(Employ employ) {
        //根据id是否存在查看是修改还是添加
        if (!StringUtils.isEmpty(employ.getId())) {
            //修改
            Employ em = data.modifyEmploy(employIndex);
            //id不可能被修改所以不用修改
            em.setGender(employ.getGender());
            em.setName( employ.getName());
            em.setDepartment(employ.getDepartment());
            em.setBirth(employ.getBirth());
            em.setEmail(employ.getEmail());
            return "redirect:/employ/list";
        }
        System.out.println(employ);
        //添加员工
        Employ employ1 = new Employ(
                employ.getName(),
                employ.getGender(),
                employ.getDepartment(),
                employ.getBirth(),
                employ.getEmail()
        );
        data.addEmploy(employ1);
        //重定向给getAllEmploys处理
        return "redirect:/employ/list";
    }

    /**
     * 修改员工
     * @author xxl
     * @param index
     * @return String
     */
    private int employIndex = 0;
    @GetMapping("modify/{index}")
    public String modify(@PathVariable("index")int index,Model model) {
        employIndex = index;
        System.out.println(index);
        //存取用户信息
        model.addAttribute(FiledConstant.EMPLOY_INFO,data.modifyEmploy(index));
        return "/page/modifyEmploy";
    }

    @GetMapping("delete/{index}")
    public String deleteEmploy(@PathVariable("index") int index) {
        data.deleteEmploy(index);
        return "redirect:/employ/list";
    }
}
